﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;

    public class PreferenceProfileDetailDA : IPreferenceProfileDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string PREFERENCE_ID = "@PREFERENCE_ID";
        private string PREFERENCE_PROFILE_ID = "@PREFERENCE_PROFILE_ID";
        private string PREFERENCE_VALUE = "@PREFERENCE_VALUE";
        private string SQL_CHANGE_ALL_PREFERENCEVALUE_BY_ID = "UPDATE SYSADM.PREFERENCE_PROFILE_DETAIL SET PREFERENCE_VALUE = @PREFERENCE_VALUE,UPDATED_BY = @UPDATED_BY,UPDATED_DATE = GETDATE() WHERE PREFERENCE_ID = @PREFERENCE_ID";
        private string SQL_CHECK_PREFERENCE_PROFILE_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM SYSADM.PREFERENCE_PROFILE_DETAIL WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID AND PREFERENCE_ID = @PREFERENCE_ID ";
        private string SQL_DELETE_PREFERENCE_PROFILE_DETAIL = " DELETE FROM SYSADM.PREFERENCE_PROFILE_DETAIL WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID ";
        private string SQL_INSERT_PREFERENCE_PROFILE_DETAIL = " INSERT INTO SYSADM.PREFERENCE_PROFILE_DETAIL ( PREFERENCE_PROFILE_ID, PREFERENCE_ID, PREFERENCE_VALUE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @PREFERENCE_PROFILE_ID, @PREFERENCE_ID, @PREFERENCE_VALUE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_PREFERENCE_PROFILE_DETAIL = " SELECT  ROWID, PREFERENCE_PROFILE_ID, PREFERENCE_ID, PREFERENCE_VALUE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  SYSADM.PREFERENCE_PROFILE_DETAIL ";
        private string SQL_UPDATE_PREFERENCE_PROFILE_DETAIL = " UPDATE  SYSADM.PREFERENCE_PROFILE_DETAIL SET PREFERENCE_VALUE = @PREFERENCE_VALUE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID AND PREFERENCE_ID = @PREFERENCE_ID  ";
        private string UPDATED_BY = "@UPDATED_BY";

        public bool CheckPreferenceProfileDetailIDUnique(string preferenceProfileDetailID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileDetailID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_PREFERENCE_PROFILE_DETAIL_ID_UNIQUE, paras).ToString() == "1");
        }

        public bool CheckPreferenceProfileDetailIDUnique(DataBase dataBase, DbTransaction tran, string preferenceProfileDetailID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileDetailID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_PREFERENCE_PROFILE_DETAIL_ID_UNIQUE, paras).ToString() == "1");
        }

        public int DeletePreferenceProfileDetail(string preferenceProfileDetailID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREFERENCE_PROFILE_DETAIL, paras);
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeletePreferenceProfileDetail(DataBase dataBase, DbTransaction tran, string preferenceProfileDetailID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceProfileDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_PREFERENCE_PROFILE_DETAIL, paras);
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public List<PreferenceProfileDetailInfo> GetPreferenceProfileDetailByID(string preferenceprofiledetailID)
        {
            string sql = this.SQL_SELECT_ALL_PREFERENCE_PROFILE_DETAIL + " WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceprofiledetailID) };
            List<PreferenceProfileDetailInfo> preferenceProfileDetailInfoList = new List<PreferenceProfileDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    PreferenceProfileDetailInfo preferenceProfileDetailInfo = new PreferenceProfileDetailInfo();
                    preferenceProfileDetailInfo = new PreferenceProfileDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["PREFERENCE_PROFILE_ID"].ToString(), reader["PREFERENCE_ID"].ToString(), reader["PREFERENCE_VALUE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    preferenceProfileDetailInfoList.Add(preferenceProfileDetailInfo);
                }
            }
            return preferenceProfileDetailInfoList;
        }

        public PreferenceProfileDetailInfo GetPreferenceProfileDetailByID(DataBase dataBase, DbTransaction tran, string preferenceprofiledetailID)
        {
            string sql = this.SQL_SELECT_ALL_PREFERENCE_PROFILE_DETAIL + " WHERE PREFERENCE_PROFILE_ID = @PREFERENCE_PROFILE_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_PROFILE_ID", preferenceprofiledetailID) };
            PreferenceProfileDetailInfo preferenceProfileDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                preferenceProfileDetailInfo = new PreferenceProfileDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["PREFERENCE_PROFILE_ID"].ToString(), reader["PREFERENCE_ID"].ToString(), reader["PREFERENCE_VALUE"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return preferenceProfileDetailInfo;
        }

        public int InsertPreferenceProfileDetail(PreferenceProfileDetailInfo preferenceProfileDetailInfo)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_PreferenceProfileDetail_Parameters(preferenceProfileDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREFERENCE_PROFILE_DETAIL, paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertPreferenceProfileDetail(DataBase dataBase, DbTransaction tran, PreferenceProfileDetailInfo preferenceProfileDetailInfo)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_PreferenceProfileDetail_Parameters(preferenceProfileDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_PREFERENCE_PROFILE_DETAIL, paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_PreferenceProfileDetail_Parameters(PreferenceProfileDetailInfo preferenceProfileDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.PREFERENCE_PROFILE_ID, SqlDbType.VarChar, 50), new SqlParameter(this.PREFERENCE_ID, SqlDbType.VarChar, 50), new SqlParameter(this.PREFERENCE_VALUE, SqlDbType.VarChar, 50), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = preferenceProfileDetailInfo.PreferenceProfileID;
            paramArray[1].Value = preferenceProfileDetailInfo.PreferenceID;
            if (!string.IsNullOrEmpty(preferenceProfileDetailInfo.PreferenceValue))
            {
                paramArray[2].Value = preferenceProfileDetailInfo.PreferenceValue;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = preferenceProfileDetailInfo.CreatedBy;
            if (!string.IsNullOrEmpty(preferenceProfileDetailInfo.UpdatedBy))
            {
                paramArray[4].Value = preferenceProfileDetailInfo.UpdatedBy;
                return paramArray;
            }
            paramArray[4].Value = DBNull.Value;
            return paramArray;
        }

        public int UpdateAllPreferenceProfileInfoByID(DataBase dataBase, DbTransaction tran, string preferenceID, string preferenceValue, string updatedBy)
        {
            string sql = this.SQL_CHANGE_ALL_PREFERENCEVALUE_BY_ID;
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@PREFERENCE_VALUE", preferenceValue), new SqlParameter("@UPDATED_BY", updatedBy), new SqlParameter("@PREFERENCE_ID", preferenceID) };
            return dataBase.ExecuteNonQuery(tran, CommandType.Text, sql, paras);
        }
    }
}

